package com.d3works.apps.hr.web;

import com.d3works.apps.base.callable.ControllerCallableTool;
import com.d3works.apps.base.db.Batch;
import com.d3works.apps.hr.db.HrBatch;
import com.d3works.apps.hr.service.batch.HrBatchService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;

@Singleton
public class HrBatchController {

  /**
   * Launch any type of HR batch
   *
   * @param request
   * @param response
   * @throws D3worksException
   */
  public void launchHrBatch(ActionRequest request, ActionResponse response) {

    try {
      HrBatch hrBatch = request.getContext().asType(HrBatch.class);
      HrBatchService hrBatchService = Beans.get(HrBatchService.class);
      hrBatchService.setBatchModel(hrBatch);

      ControllerCallableTool<Batch> batchControllerCallableTool = new ControllerCallableTool<>();
      Batch batch = batchControllerCallableTool.runInSeparateThread(hrBatchService, response);

      if (batch != null) {
        response.setInfo(batch.getComments());
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    } finally {
      response.setReload(true);
    }
  }
}
